package com.cyf.merchat;

import com.cyf.app.network.bean.AppBusinessRsp;
import com.cyf.app.network.bean.AppGetCouponReq;
import com.cyf.app.network.bean.AppGetCouponRsp;
import com.cyf.app.service.AppService;
import com.cyf.db.bean.MerchantCoupon;
import com.cyf.merchat.network.bean.*;
import com.cyf.merchat.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商户请求入口
 * Created by cage on 2017/3/29.
 */
@RestController
@RequestMapping(value = "merchant")
@Api(value = "商户相关api")
public class MerchantHttpController {

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private AppService appService;


    @ApiOperation(value = "商户登录")
    @PostMapping(value = "/login")
    public MerchantLoginRsp login(@RequestBody MerchantLoginReq req) {
        return merchantService.login(req);
    }

    @ApiOperation(value = "app商户登录")
    @PostMapping(value = "/appLogin")
    public MerchantLoginRsp appLogin(@RequestBody MerchantLoginReq req) {
        return merchantService.appLogin(req);
    }

    @ApiOperation(value = "退出登录")
    @GetMapping(value = "/signOutLogin")
    public MerchantLoginRsp signOutLogin(@RequestParam Integer merchantId) {
        return merchantService.signOutLogin(merchantId);
    }



    @ApiOperation(value = "切换车场")
    @GetMapping(value = "/switchParking")
    public MerchantLoginRsp switchParking(@RequestParam Integer merchantId) {
        return merchantService.switchParking(merchantId);
    }

    @ApiOperation(value = "检查是否是app用户")
    @PostMapping(value = "/checkIsAppUserByMobile")
    public MerchantCheckAppUserByMobileRsp checkIsAppUserByMobile(@RequestBody MerchantCheckAppUserByMobileReq req) {
        return merchantService.checkIsAppUserByMobile(req);
    }

    @ApiOperation(value = "保存优惠券信息")
    @PostMapping(value = "/saveCouponInfo")
    public MerchantSaveCouponInfoRsp saveCouponInfo(@RequestBody MerchantSaveCouponInfoReq req) {
        return merchantService.saveCouponInfo(req);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/updatePwd")
    public MerchantUpdatePwdRsp updatePwdRsp(@RequestBody MerchantUpdatePwdReq req) {
        return merchantService.updatePwdRsp(req);
    }

    @ApiOperation(value = "商户查询优惠券发放记录")
    @PostMapping(value = "/issueRecord")
    public MerchantCouponLssueRecordRsp issueRecord(@RequestBody MerchantCouponLssueRecordReq req) {
        return merchantService.issueRecord(req);
    }

    @ApiOperation(value = "回收优惠券")
    @PostMapping(value = "/takeBack")
    public AppBusinessRsp takeBack(@RequestParam String ids) {
        return merchantService.takeBack(ids);
    }


    @ApiOperation(value = "获取优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "couponStatus", value = "优惠券状态: 1停车场券 2车易付券 3商家券 0失效",
                    dataType = "int", required = true, paramType = "query")
    })
    @PostMapping(value = "/getCoupon")
    public AppGetCouponRsp getCoupon(@RequestParam String token,
                                     @RequestParam int couponStatus) {
        AppGetCouponReq req = new AppGetCouponReq();
        req.setToken(token);
        req.setCouponStatus(couponStatus);
        return appService.getCoupon(req);
    }


    @ApiOperation(value = "获取当前车场的优惠规则列表")
    @PostMapping(value = "/parkingFeegroupList")
    public MerchantFeegroupRsp parkingMerchatFeegroup(@RequestParam Integer parkingId, @RequestParam Integer merchatId) {

        return merchantService.parkingFeegroup(parkingId, merchatId);
    }

    @ApiOperation(value = "获取单个优惠规则")
    @PostMapping(value = "/parkingByFeegroupId")
    public MerchantFeegroupRsp parkingFeegroup(@RequestParam Integer parkingId,
                                               @RequestParam Integer feeGroupId) {

        return merchantService.parkingByFeegroupId(parkingId, feeGroupId);
    }

    @ApiOperation(value = "生成优惠券")
    @PostMapping(value = "/saveCoupon")
    public MerchantBusinessRsp saveCoupon(@RequestBody SaveCouponReq saveCouponReq) {
        MerchantBusinessRsp merchantBusinessRsp = new MerchantBusinessRsp();
        merchantBusinessRsp.setResult(MerchantBusinessRsp.SUCCESS);
        try {
            //线上使用 手机号必填
            if (saveCouponReq.getUseWay() != null && saveCouponReq.getUseWay().equals(MerchantCoupon.USEWAY_ONLINE)) {
                if (StringUtils.isEmpty(saveCouponReq.getMobile())) {
                    merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
                    merchantBusinessRsp.setStrError("手机号必填!");
                    return merchantBusinessRsp;
                }
            }
            //线下使用 车牌号必填
            if (saveCouponReq.getUseWay() != null && saveCouponReq.getUseWay().equals(MerchantCoupon.USEWAY_OFFLINE)) {
                if (StringUtils.isEmpty(saveCouponReq.getPlate())) {
                    merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
                    merchantBusinessRsp.setStrError("车牌号必填!");
                    return merchantBusinessRsp;
                }
            }
            //线上发放
            if (saveCouponReq.getUseWay() != null && saveCouponReq.getUseWay().equals(MerchantCoupon.USEWAY_ONLINE)) {
                // 调取验证手机号是否为APP用户接口
                MerchantCheckAppUserByMobileReq merchantCheckAppUserByMobileReq = new MerchantCheckAppUserByMobileReq();
                merchantCheckAppUserByMobileReq.setMobile(saveCouponReq.getMobile());
                merchantCheckAppUserByMobileReq.setParkingId(saveCouponReq.getParkingId());
                MerchantCheckAppUserByMobileRsp merchantCheckAppUserByMobileRsp = checkIsAppUserByMobile(merchantCheckAppUserByMobileReq);
                if (merchantCheckAppUserByMobileRsp.getResult() == -1) {
                    // 核验手机号 接口出错
                    merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
                    merchantBusinessRsp.setStrError("核验手机号 接口出错");
                    return merchantBusinessRsp;
                } else if (merchantCheckAppUserByMobileRsp.getSubResult() == 0) {
                    // 手机号非App用户
                    merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
                    merchantBusinessRsp.setStrError("手机号非App用户");
                    return merchantBusinessRsp;
                }
            }
            if (saveCouponReq.getMerchantId() == null) {
                merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
                merchantBusinessRsp.setStrError("商户不能为空！");
                return merchantBusinessRsp;
            }
            if (saveCouponReq.getIssueNumber() == null) {
                merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
                merchantBusinessRsp.setStrError("发行张数不能为空！");
                return merchantBusinessRsp;
            }
            // 调取提交二维码信息接口
            if (saveCouponReq.getMerchantId() != null && saveCouponReq.getParkingId() != null) {
                MerchantSaveCouponInfoRsp merchantSaveCouponInfoRsp = merchantService.saveCoupon(saveCouponReq);
                merchantBusinessRsp.setItem(merchantSaveCouponInfoRsp.getItem());
                merchantBusinessRsp.setCouponIds(merchantSaveCouponInfoRsp.getCouponIds());
                if (merchantSaveCouponInfoRsp.getResult() == -1) {
                    // 发放优惠券失败
                    merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
                    merchantBusinessRsp.setStrError(merchantSaveCouponInfoRsp.getStrError());
                    return merchantBusinessRsp;
                }
                if (merchantSaveCouponInfoRsp.getResult() == -100) {
                    merchantBusinessRsp.setResult(MerchantBusinessRsp.RESULT_COUPON_MAX);
                    merchantBusinessRsp.setStrError(merchantSaveCouponInfoRsp.getStrError());
                    return merchantBusinessRsp;
                }
            }
        } catch (Exception e) {
            merchantBusinessRsp.setResult(MerchantBusinessRsp.FAIL);
            merchantBusinessRsp.setStrError("发放优惠券异常");
            return merchantBusinessRsp;
        }
        return merchantBusinessRsp;
    }

    @ApiOperation(value = "商户充值记录")
    @PostMapping(value = "/merchantRecharge")
    public MerchantFeegroupRsp merchantRecharge(@RequestParam Integer parkingId,
                                                @RequestParam Integer merchantId) {
        return merchantService.merchantRecharge(parkingId, merchantId);
    }


    @ApiOperation(value = "生成商户二维码")
    @PostMapping(value = "/qrCodeGeneration")
    public MerchantQrCodeGenerationRsp qrCodeGeneration(@RequestParam Integer merchantId) {
        return merchantService.qrCodeGeneration(merchantId);
    }

    @ApiOperation(value = "作废之前二维码")
    @PostMapping(value = "/invalidQRCode")
    public MerchantInvalidQRCodeRsp invalidQRCode(@RequestParam Integer merchantId) {
        return merchantService.invalidQRCode(merchantId);
    }
}
